package servlets;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modelo.Cliente;
import modelo.Local;

import dao.LocalDAO;
import excepcion.DAOExcepcion;

/**
 * Servlet implementation class ListaLocalesServlet
 */
@WebServlet("/ListaLocalesServlet")
public class ListaLocalesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListaLocalesServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession();
		if ((Cliente) sesion.getAttribute("cliente") != null) {
			try {
				Collection<Local> locales = (Collection<Local>) ((new LocalDAO())
						.Listar());
				request.setAttribute("locales", locales);
			} catch (DAOExcepcion e) {
				e.printStackTrace();
			}
			RequestDispatcher vista = request.getRequestDispatcher("ListaLocales.jsp");
            vista.forward(request, response);
		} else {
			response.sendRedirect("Logeo.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
